package com.ruifu.pls.utils;

import com.ruifu.pls.model.GLCallInputParam;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SighUtil {


    public static String  signParam(GLCallInputParam request)
    {
        StringBuffer param =new StringBuffer();
        param.append(request.getApp_secret());
        param.append("app_key");
        param.append(request.getApp_key());
        param.append("data");
        param.append(request.getData());
        param.append("method");
        param.append(request.getMethod());
        param.append("timestamp");
        param.append(request.getTimestamp());
        param.append(request.getApp_secret());

        return param.toString();
    }

    public static String  createSignStr(GLCallInputParam glRquestParam)
    {
       return getMD5(signParam(glRquestParam));
    }


    public static String getMD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        try {
            byte[] btInput = s.getBytes("utf-8");
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    public  static String objectToJson(BaseModule baseModule)
//    {
//        net.sf.json.JSONObject jsonObject =
//
//    }

    public  static void main(String aargs[])
    {
        try
        {
            SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
            String date= sdf.format(new Date());
            System.out.println(date);


            String s = SighUtil.getMD5("ab179020b82d2fdcd4cea176796f7156app_keyapitestdata{\"carnum\":\"\\u4eacA09999\"}methodips2.api.currentStatustimestamp2012-09-19 19:12:05ab179020b82d2fdcd4cea176796f7156");
            System.out.println(s);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


}
